package m4;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {

        //连接
        ConnectionFactory f= new ConnectionFactory();
        f.setHost("192.168.64.140");
        //f.setHost("wht6.cn");
        f.setUsername("admin");
        f.setPassword("admin");

        Channel c=f.newConnection().createChannel();

        //1.创建随机队列
        String queue=UUID.randomUUID().toString();
        //false非持久，true独占，true自动删除
        c.queueDeclare(queue,false,true,true,null);
        //2.创建交换机
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        //3.绑定

        System.out.println("输入绑定键，用空格隔开：");
        String s=new Scanner(System.in).nextLine();
           String[]a= s.split("\\s+");

           for (String key : a){
               c.queueBind(queue, "direct_logs", key);
           }

        //从随机队列接收消息
        //1.收到消息
        DeliverCallback deliverCallback=new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s = new String(message.getBody());
                System.out.println("收到：" + s);
            }
        };

       //2.取消消息
              CancelCallback cancelCallback= new CancelCallback(){
           @Override
           public void handle(String consumerTag) throws IOException {

           }
       };
         c.basicConsume(queue, true, deliverCallback,cancelCallback);
    }
}
